package MyFitnessPlate.Handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedFoodDataSet;
@SuppressWarnings("unused")
public class FoodHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	private String categoryType = null;
	private boolean in_xml = false;
	private boolean in_type = false;
	private boolean in_name = false;
	private boolean in_entries = false;
	private boolean in_food = false;
	private boolean in_entry_id = false;
	private boolean in_id = false;
	private boolean in_servings = false;
	private boolean in_calories = false;
	private boolean in_fat_calories = false;
	private boolean in_fat = false;
	private boolean in_fat_unit = false;
	private boolean in_saturated_fat = false;
	private boolean in_saturated_fat_unit = false;
	private boolean in_trans_fat = false;
	private boolean in_trans_fat_unit = false;
	private boolean in_cholesterol = false;
	private boolean in_cholesterol_unit = false;
	private boolean in_sodium = false;
	private boolean in_sodium_unit = false;
	private boolean in_carbohydrates = false;
	private boolean in_carbohydrates_unit = false;
	private boolean in_fiber = false;
	private boolean in_fiber_unit = false;
	private boolean in_sugar = false;
	private boolean in_sugar_unit = false;
	private boolean in_protein = false;
	private boolean in_protein_unit = false;
	private ArrayList<ParsedFoodDataSet> myParsedFoodDataSet;// = new ArrayList<ParsedFoodDataSet>(0);

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ArrayList<ParsedFoodDataSet> getParsedData() {
		return this.myParsedFoodDataSet;
	}
	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedFoodDataSet = new ArrayList<ParsedFoodDataSet>();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("type")) {
			this.in_type = true;
		} else if (localName.equals("entries")) {
			this.in_entries = true;
		} else if (localName.equals("name")) {
			this.in_name = true;
		} else if (localName.equals("food")) {
			this.in_food = true;
		} else if (localName.equals("entry-id")) {
			this.in_entry_id = true;
		} else if (localName.equals("id")) {
			this.in_id = true;
		} else if (localName.equals("servings")) {
			this.in_servings = true;
		} else if (localName.equals("calories")) {
			this.in_calories = true;
		} else if (localName.equals("fat-calories")) {
			this.in_fat_calories = true;
		} else if (localName.equals("fat")) {
			this.in_fat = true;
		} else if (localName.equals("fat-unit")) {
			this.in_fat_unit = true;
		} else if (localName.equals("saturated-fat")) {
			this.in_saturated_fat = true;
		} else if (localName.equals("saturated-fat-unit")) {
			this.in_saturated_fat_unit = true;
		} else if (localName.equals("trans-fat")) {
			this.in_trans_fat = true;
		} else if (localName.equals("trans-fat-unit")) {
			this.in_trans_fat_unit = true;
		} else if (localName.equals("cholesterol")) {
			this.in_cholesterol = true;
		} else if (localName.equals("cholesterol-unit")) {
			this.in_cholesterol_unit = true;
		} else if (localName.equals("sodium")) {
			this.in_sodium = true;
		} else if (localName.equals("sodium-unit")) {
			this.in_sodium_unit = true;
		} else if (localName.equals("carbohydrates")) {
			this.in_carbohydrates = true;
		} else if (localName.equals("carbohydrates-unit")) {
			this.in_carbohydrates_unit = true;
		} else if (localName.equals("fiber")) {
			this.in_fiber = true;
		} else if (localName.equals("fiber-unit")) {
			this.in_fiber_unit = true;
		} else if (localName.equals("sugar")) {
			this.in_sugar = true;
		} else if (localName.equals("sugar-unit")) {
			this.in_sugar_unit = true;
		} else if (localName.equals("protein")) {
			this.in_protein = true;
		} else if (localName.equals("protein-unit")) {
			this.in_protein_unit = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("type")) {
			this.in_type = false;
		} else if (localName.equals("entries")) {
			this.in_entries = false;
		} else if (localName.equals("name")) {
			this.in_name = false;
		} else if (localName.equals("food")) {
			this.in_food = false;
		} else if (localName.equals("entry-id")) {
			this.in_entry_id = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("servings")) {
			this.in_servings = false;
		} else if (localName.equals("calories")) {
			this.in_calories = false;
		} else if (localName.equals("fat-calories")) {
			this.in_fat_calories = false;
		} else if (localName.equals("fat")) {
			this.in_fat = false;
		} else if (localName.equals("fat-unit")) {
			this.in_fat_unit = false;
		} else if (localName.equals("saturated-fat")) {
			this.in_saturated_fat = false;
		} else if (localName.equals("saturated-fat-unit")) {
			this.in_saturated_fat_unit = false;
		} else if (localName.equals("trans-fat")) {
			this.in_trans_fat = false;
		} else if (localName.equals("trans-fat-unit")) {
			this.in_trans_fat_unit = false;
		} else if (localName.equals("cholesterol")) {
			this.in_cholesterol = false;
		} else if (localName.equals("cholesterol-unit")) {
			this.in_cholesterol_unit = false;
		} else if (localName.equals("sodium")) {
			this.in_sodium = false;
		} else if (localName.equals("sodium-unit")) {
			this.in_sodium_unit = false;
		} else if (localName.equals("carbohydrates")) {
			this.in_carbohydrates = false;
		} else if (localName.equals("carbohydrates-unit")) {
			this.in_carbohydrates_unit = false;
		} else if (localName.equals("fiber")) {
			this.in_fiber = false;
		} else if (localName.equals("fiber-unit")) {
			this.in_fiber_unit = false;
		} else if (localName.equals("sugar")) {
			this.in_sugar = false;
		} else if (localName.equals("sugar-unit")) {
			this.in_sugar_unit = false;
		} else if (localName.equals("protein")) {
			this.in_protein = false;
		} else if (localName.equals("protein-unit")) {
			this.in_protein_unit = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String characterString = new String(ch, start, length);
		if (this.in_entry_id) {
			//Add a new element to the ArrayList.
			myParsedFoodDataSet.add(new ParsedFoodDataSet());
			myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setEntry_id(characterString.trim());
		}
		if (this.in_id) {
			myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setId(characterString);
		}
		if (this.in_name) {
			//This is a way to attempt to fix the name information because the tag was used twice.
			if (characterString.equals("Breakfast")){
				this.categoryType = "1";
			} else if (characterString.equals("Lunch")){
				this.categoryType = "2";
			} else if (characterString.equals("Dinner")){
				this.categoryType = "3";
			} else if (characterString.equals("Snacks")){
				this.categoryType = "4";
			} else if (characterString.equals("Supplements")){
				this.categoryType = "5";
			} else {
				myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setCategory(this.categoryType);
				myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setName(characterString);
			}
		}
		if (this.in_servings) {
			myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setServings(characterString);
		}
		if (this.in_calories) {
			myParsedFoodDataSet.get(myParsedFoodDataSet.size()-1).setCalories(characterString);
		}
	}
}